﻿using AnChao.WPF.Controls.MDI.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace CRootGUI.Controls
{
    public class TabControlDraggable: System.Windows.Controls.TabControl
    {
        #region 可以操作右键,窗外窗等

        public static readonly DependencyProperty CanOperateProperty = DependencyProperty.Register("CanOperate", typeof(bool), typeof(TabControlDraggable),
                new FrameworkPropertyMetadata(true));

        /// <summary>
        /// CanOperate
        /// </summary>
        public bool CanOperate
        {
            get { return (bool)GetValue(CanOperateProperty); }
            set { SetValue(CanOperateProperty, value); }
        }

        #endregion

        public TabControlDraggable()
        {
            this.Drop += TabControlDraggable_Drop; 
        }

        private void TabControlDraggable_Drop(object sender, System.Windows.DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(TabItemDraggable)))
            {
                var sourceItem = e.Data.GetData(typeof(TabItemDraggable)) as TabItemDraggable;
                if (sourceItem == null)
                {
                    return;
                }      

                var sourceparent = System.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(sourceItem) as System.Windows.Controls.TabControl;
                if (sourceparent != null)
                {
                    if (sourceparent == this) return;

                    //if (Cubic.Wizard.GroupWindow.IgnoreUnloadedControls.Any(p => p.IsDragOut && p.GetType().Name == sourceItem.Content.GetType().Name))
                    //{
                    //    CRootGUI.Common.MessageBox.Show("已经存在弹出界面,不允许再次弹出");
                    //    return;
                    //}

                    var source = sourceparent.ItemContainerGenerator.ItemFromContainer(sourceItem);
                    IList soureclist = sourceparent.GetActualList();
                    soureclist.Remove(source);
                    if(soureclist.Count ==0)
                    {
                        sourceparent.MinTabControl();
                    }
                    else
                    {
                        sourceparent.SelectedIndex = 0;
                    }

                    IList list = this.GetActualList();
                    list.Add(source);

                    sourceItem.IsSelected = true;
                    if (sourceItem.OriParent == null)
                    {
                        sourceItem.OriParent = sourceparent;
                    }
                   
                }
            }
        
        }

        public Window WindowBase { get; set; }

        public void SetClosedAction(Window windowBase)
        {
            WindowBase = windowBase;
            WindowBase.Closed += WindowBase_ClosedAction;
        }

        private void WindowBase_ClosedAction(object sender, EventArgs e)
        {
            WindowBase.Closed -= WindowBase_ClosedAction;


            foreach(var item in Items.OfType<TabItemDraggable>().ToList())
            {
                var sourceparent = System.Windows.Controls.ItemsControl.ItemsControlFromItemContainer(item) as System.Windows.Controls.TabControl;
                if (sourceparent != null)
                {
                    var source = sourceparent.ItemContainerGenerator.ItemFromContainer(item);
                    IList soureclist = sourceparent.GetActualList();
                    soureclist.Remove(source);
                }
                if (item.OriParent != null)
                {
                    if (item.OriParent.Items.Count == 0)
                    {
                        item.OriParent.ResetTabControl();
                    }
                    item.OriParent.Items.Add(item);
                    item.IsSelected = true;
                    
             
                }      
            }
        }
    }
}
